#include <p24F16KA102.h>

// PIC24F16KA102 Configuration bits
/*
** --------------------------------------------------------------------
** _FBS (Register FBS: 0xF80000)
**  BWRP_OFF - Boot Segment Write Protect Disable
**  BSS_OFF  - Boot Segment Code Protect Disable
** --------------------------------------------------------------------
** _FGS (Register FGS: 0xF80004)
**  GWRP_OFF - General Segment Write Protect Disable
**  GCP_OFF  - General Segment Code Protect Disable
** --------------------------------------------------------------------
** _FOSCSEL (Register FOSCSEL: 0xF80006)
**  FNOSC_FRCDIV - Fast RC Oscillator With Postscale (FRCDIV)
**  IESO_OFF - Two Speed Start-up Disable
** --------------------------------------------------------------------
** _FOSC (Register FOSC: 0xF80008)
**  FCKSM_CSECMD - Clock Switching Enabled, Clock Monitor Disabled
**  POSCFREQ_MS  - Mid Speed (100kHz - 8MHz)
**  OSCIOFNC_ON  - OSCO Pin Has Digital I/O Function (RA3)
**  POSCMOD_NONE - Primary disabled
**  SOSCSEL_SOSCLP - Low Power Secondary Oscillator
** --------------------------------------------------------------------
** _FWDT (Register FWDT (0xF8000A)
**  FWDTEN_OFF - Watchdog Timer (WDT) Disable
**  WINDIS_OFF - Non-Window WDT Mode
**  FWPSA_PR128 - WDT Prescale 1:128
**  WDTPS_PS32768 - WDT Postscale 1:32,768
** --------------------------------------------------------------------
** _FPOR (Register FPOR: 0xF8000C)
**  MCLRE_OFF - MCLR Disabled, RA5 Enabled
**  BORV_LPBOR - Low Power BOR (Brown Out Voltage)
**  BOREN_BOR3 - Enabled in hardware, SBOREN bit disabled
**  I2C1SEL_PRI - Use SCL1/SDA1 Pins For I2C1
**  PWRTEN_OFF - Power Up Timer Disabled
** --------------------------------------------------------------------
** _FICD (Register FICD: 0xF8000E)
**  BKBUG_OFF - Background Debugger Disabled
**  ICS_PGx3 - EMUC/EMUD share PGC3/PGD3
** --------------------------------------------------------------------
** _FDS (Register FDS: 0xF80010)
**  DSWDTEN_OFF - Deep Sleep WDT Disabled
**  DSBOREN_ON - Deep Sleep BOR Enabled
**  RTCOSC_SOSC - RTCC Oscillator select: Secondary Oscillator (SOSC)
**  DSWDTOSC_SOSC - Deep Sleep WDT Low Power RC Oscillator (LPRC)
**  DSWDTPS_DSWDTPSF - Deep Sleep WDT Postscale 1:2,147,483,648
**                     (25.7 Days)
** --------------------------------------------------------------------
*/

/*
_FBS(BWRP_OFF & BSS_OFF)
_FGS(GWRP_OFF & GCP_OFF)
_FOSCSEL(FNOSC_FRCDIV & IESO_OFF)
_FOSC(FCKSM_CSECMD & POSCFREQ_MS & OSCIOFNC_ON & POSCMOD_NONE & SOSCSEL_SOSCLP)
_FWDT(FWDTEN_OFF & WINDIS_OFF & FWPSA_PR128 & WDTPS_PS32768)
_FPOR(MCLRE_ON & BORV_LPBOR & BOREN_BOR3 & I2C1SEL_PRI & PWRTEN_OFF)
_FICD(BKBUG_OFF & ICS_PGx3)
_FDS(DSWDTEN_OFF & DSBOREN_ON & RTCOSC_SOSC & DSWDTOSC_SOSC & DSWDTPS_DSWDTPSF) */
_FICD(ICS_PGx3)

//External functions that may/may not be used in main():
void ledBlink(void);
void ledButton(void);

static void setup(void)
{
	// Use Internal 8 MHz Clock
	OSCCON = 0x0000;
	CLKDIV = 0x0000;

	// Configure IC PWR (Power controller for board)
	TRISBbits.TRISB2  = 0; //Set IC PWR as output
    LATBbits.LATB2    = 0; //Enable power for the board

	// Enable switch pullups
	CNPU2bits.CN23PUE = 1; //Set pull-up for CN23 == RB7 == Switch 2
	CNPU1bits.CN12PUE = 1; //Set pull-up for CN12 == RB14 == Switch 3

	// Set A/D input ports to digital mode
	AD1PCFGbits.PCFG10 = 1; //AN10 == RB14 == Switch 3 for Red LED
}

static void runSuite(void)
{
	//Comment/Uncomment functions here:
	//ledBlink();
	ledButton();
}

int main(void)
{
	setup();
	runSuite();

	return 0;
}
